package com.higgins.eventtracking.client.health.fundamentals;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.health.PopupQuestionBox;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;

public class DigestionTab extends VerticalPanel	implements GenericTabInterface	{
	private final EventTrackingServiceAsync greetingService = GWT
										.create(EventTrackingService.class);
	private HorizontalPanel rowOne = new HorizontalPanel();
	private HorizontalPanel rowTwo = new HorizontalPanel();
	private HorizontalPanel rowThree = new HorizontalPanel();
	private HorizontalPanel rowFour = new HorizontalPanel();
	private Label daysSinceRoughage = new Label();
	private Label daysSinceFruit = new Label();
	private Label daysSinceVeg = new Label();
	private Label daysSinceSalad = new Label();
	private Label daysSinceExercise = new Label();		// Implement
	private Label daysSinceStretch = new Label();
	private Label daysSinceSleepDig = new Label();
	private Label hoursSinceWater = new Label();
	private Label hoursSinceFood = new Label();			// Implement
	private Label firstEaten = new Label();				// Implement
	private Label blank = new Label();
	private Label blank2 = new Label();
	private Label blank4 = new Label();
	private Label daysSinceBloated = new Label();		// Double chek
	private Label daysSinceBisacodyl = new Label();
	private boolean firstRun = true;
//	private boolean parentAlertTriggered = false;
	private String tabTitle = null;
	private Integer tabIndex = null;
//	private Boolean alert1 = null;
//	private Boolean alert2 = null;
//	private Boolean alert3 = null;
//	private Boolean alert4 = null;
//	private Boolean alert5 = null;
//	private Boolean alert6 = null;
//	private Boolean alert7 = null;
//	private Boolean alert8 = null;
	private Fundamentals parentTab = null;
	private PopupQuestionBox popupBox = new PopupQuestionBox();
	private Integer height = 20;
	private Integer width = 175;
	
	public DigestionTab(Fundamentals parentTab)	{
		this.parentTab = parentTab;
		setHeight("600px");
		setWidth("800px");
		setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
	}
	public void update()	{
		if (firstRun)
			initialise();
	}
	public void initialise()	{
		setLabelWidths();
		setLabelHeight();
		addFields();
		populateFields();
		firstRun = false;
		popupBox.checkForQuestions();
	}
	
	private void addFields()	{
		addRowOne();
		addRowTwo();
		addRowThree();
		addRowFour();
		//setHeight("100%");		
	}
	
	private void addRowOne()	{
		this.setSpacing(1);
		rowOne.setSpacing(3);
		setCellHeight(firstEaten, Integer.toString(firstEaten.getElement().getOffsetHeight())+"px");
		rowOne.add(firstEaten);
		rowOne.setCellHeight(daysSinceRoughage, Integer.toString(daysSinceRoughage.getElement().getOffsetHeight())+"px");
		rowOne.add(daysSinceRoughage);
		rowOne.setCellHeight(daysSinceExercise, Integer.toString(daysSinceExercise.getElement().getOffsetHeight())+"px");
		rowOne.add(daysSinceExercise);
		rowOne.setCellHeight(daysSinceBloated, Integer.toString(daysSinceBloated.getElement().getOffsetHeight())+"px");		
		rowOne.add(daysSinceBloated);
		add(rowOne);
	}
	private void addRowTwo()	{
		rowTwo.setSpacing(3);
		rowTwo.setCellHeight(hoursSinceWater, Integer.toString(hoursSinceWater.getElement().getOffsetHeight()) +"px");
		rowTwo.add(hoursSinceWater);
		rowTwo.setCellHeight(daysSinceFruit, Integer.toString(daysSinceFruit.getElement().getOffsetHeight()) +"px");
		rowTwo.add(daysSinceFruit);
		rowTwo.setCellHeight(daysSinceStretch, Integer.toString(daysSinceStretch.getElement().getOffsetHeight()) +"px");
		rowTwo.add(daysSinceStretch);
		rowTwo.setCellHeight(daysSinceBisacodyl, Integer.toString(daysSinceBisacodyl.getElement().getOffsetHeight()) + "px");
		rowTwo.add(daysSinceBisacodyl);
		add(rowTwo);
	}
	private void addRowThree()	{
		rowThree.setSpacing(3);
		rowThree.setCellHeight(hoursSinceFood, Integer.toString(hoursSinceFood.getElement().getOffsetHeight()) +"px");
		rowThree.add(hoursSinceFood);
		rowThree.setCellHeight(daysSinceVeg, Integer.toString(daysSinceVeg.getElement().getOffsetHeight()) +"px");
		rowThree.add(daysSinceVeg);
		rowThree.setCellHeight(blank, Integer.toString(blank.getElement().getOffsetHeight()) +"px");
		rowThree.add(blank);
		rowThree.setCellHeight(blank4, Integer.toString(blank4.getElement().getOffsetHeight()) +"px");
		rowThree.add(blank4);
		add(rowThree);
	}
	private void addRowFour()	{
		rowFour.setSpacing(3);
		rowFour.setCellHeight(blank2, Integer.toString(blank2.getElement().getOffsetHeight()) +"px");
		rowFour.add(blank2);
		rowFour.setCellHeight(daysSinceSalad, Integer.toString(daysSinceSalad.getElement().getOffsetHeight()) +"px");
		rowFour.add(daysSinceSalad);
		add(rowFour);
	}

	private void setLabelWidths()	{
		firstEaten.setWidth(width + "px");
		daysSinceRoughage.setWidth(width + "px");
		daysSinceExercise.setWidth(width + "px");
		blank4.setWidth(width + "px");
		
		hoursSinceWater.setWidth(width + "px");
		daysSinceFruit.setWidth(width + "px");
		daysSinceStretch.setWidth(width + "px");
		daysSinceBloated.setWidth(width + "px");

		hoursSinceFood.setWidth(width + "px");
		daysSinceVeg.setWidth(width + "px");
		blank.setWidth(width + "px");
		daysSinceBisacodyl.setWidth(width + "px");		
		
		blank2.setWidth(width + "px");
		daysSinceSalad.setWidth(width + "px");
		daysSinceSleepDig.setWidth(width + "px");
	}
	
	private void setLabelHeight()	{
		firstEaten.setHeight(height + "px");
		daysSinceRoughage.setHeight(height + "px");
		daysSinceExercise.setHeight(height + "px");
		blank4.setHeight(height + "px");
		
		hoursSinceWater.setHeight(height + "px");
		daysSinceFruit.setHeight(height + "px");
		daysSinceStretch.setHeight(height + "px");
		daysSinceBloated.setHeight(height + "px");

		hoursSinceFood.setHeight(height + "px");
		daysSinceVeg.setHeight(height + "px");
		blank.setHeight(height + "px");
		daysSinceBisacodyl.setHeight(height + "px");	
		
		blank2.setHeight(height + "px");
		daysSinceSalad.setHeight(height + "px");
		daysSinceSleepDig.setHeight(height + "px");
	}

	private void populateFields()	{
		populateDaysSinceExercise();
		populateDaysSinceStretch();
		populateDaysSinceSleep();
		populateDaysSinceSalad();
		populateDaysSinceFruit();
		populateDaysSinceVeg();
		populateDaysSinceRoughage();
		populateHoursSinceWater();
		populateHoursSinceFood();
		populateFirstEaten();
		populateDaysSinceBloated();
		populateDaysSinceBisacodyl();
	}
	
	private void populateDaysSinceExercise()	{
		greetingService.retrieveDaysSinceExercise(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert1 = false;
					}
					public void onSuccess(final Integer results) {
						daysSinceExercise.setText(results.toString() + " days since last execised");
						if (results!=null && results > 3)	{
							daysSinceExercise.addStyleName("highlightAlertLabel");
//							parentAlertTriggered = true;
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
//							alert1 = true;
//						} else {
//							alert1 = false;
						}
						daysSinceExercise.setHeight(height + "px");
					}
				});
	}
	private void populateDaysSinceStretch()	{
		greetingService.retrieveDaysSinceStretch(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert2 = false;
//						passBackToParent();
					}
					public void onSuccess(final Integer results) {
						daysSinceStretch.setText(results.toString() + " days since last stretched");
						if (results!=null && results > 3)	{
							daysSinceStretch.addStyleName("highlightAlertLabel");
//							alert2 = true;
//							parentAlertTriggered = true;
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
//						} else {
	//						alert2 = false;
						}
						daysSinceStretch.setHeight(height + "px");
//						passBackToParent();
					}
				});
	}
	private void populateDaysSinceSleep()	{
		greetingService.retrieveDaysSinceSleep(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
					}
					public void onSuccess(final Integer results) {
						daysSinceSleepDig.setText(results.toString() + " days since last sleep session");
						if (results!=null && results > 7)	{
//							parentAlertTriggered = true;
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
						}
						daysSinceSleepDig.setHeight(height + "px");
					}
				});
	}

	private void populateDaysSinceSalad()	{
		greetingService.retrieveDaysSinceSalad(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert4 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						daysSinceSalad.setText(results.toString() + " days since salad");
						if (results!=null && results > 2)	{
							daysSinceSalad.addStyleName("highlightAlertLabel");
//							parentAlertTriggered = true;
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
//							alert4 = true;
//						} else {
//							alert4 = false;
						}	
						daysSinceSalad.setHeight(height + "px");
//						passBackToParent();
					}
				});
	}	
	private void populateDaysSinceRoughage()	{
		greetingService.retrieveDaysSinceRoughage(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert5 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						if (results!=null)	{
							daysSinceRoughage.setText(results.toString() + " days since roughage");
							if (results!=null && results > 2)	{
								daysSinceRoughage.addStyleName("highlightAlertLabel");
//								parentAlertTriggered = true;
								parentTab.updateTabTitle(tabIndex, tabTitle, true);
//								alert5 = true;
//							} else {
//								alert5 = false;
							}
//							passBackToParent();
							daysSinceRoughage.setHeight(height + "px");

						}
					}
				});
	}	
	private void populateDaysSinceFruit()	{
		greetingService.retrieveDaysSinceFruit(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert6 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						if (results!=null)	{
							daysSinceFruit.setText(results.toString() + " days since fruit");
							if (results!=null && results > 2)	{
								daysSinceFruit.addStyleName("highlightAlertLabel");
//								parentAlertTriggered = true;
								parentTab.updateTabTitle(tabIndex, tabTitle, true);
//								alert6 = true;
//							} else {
//								alert6 = false;
							}
//							passBackToParent();
							daysSinceFruit.setHeight(height + "px");

						}
					}
				});
	}		
	private void populateDaysSinceVeg()	{
		greetingService.retrieveDaysSinceVeg(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert7 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						if (results!=null)	{
							daysSinceVeg.setText(results.toString() + " days since veg");
							if (results!=null && results > 2)	{
								daysSinceVeg.addStyleName("highlightAlertLabel");
//								parentAlertTriggered = true;
								parentTab.updateTabTitle(tabIndex, tabTitle, true);
//								alert7 = true;
//							} else {
//								alert7 = false;
							}
							daysSinceVeg.setHeight(height + "px");
//							passBackToParent();

						}
					}
				});
	}			
	private void populateHoursSinceWater()	{
		greetingService.retrieveHoursSinceWater(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert8 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						hoursSinceWater.setText(results!=null?results.toString() + " hours since water":"");
						if (results!=null && results > 3)	{
							hoursSinceWater.addStyleName("highlightAlertLabel");
//							parentAlertTriggered = true;
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
//							alert8 = true;
//						} else {
//							alert8 = false;
						}
						hoursSinceWater.setHeight(height + "px");
//						passBackToParent();

					}
				});
	}
	private void populateHoursSinceFood()	{
		greetingService.retrieveHoursSinceFood(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert3 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						hoursSinceFood.setText(results!=null?results.toString() + " hours since food":"");
						if (results!=null && results > 3)	{
							hoursSinceFood.addStyleName("highlightAlertLabel");
//							parentAlertTriggered = true;
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
//							alert3 = true;
//						} else {
//							alert3 = false;
						}
						hoursSinceFood.setHeight(height + "px");
//						passBackToParent();

					}
				});
	}
	private void populateFirstEaten()	{
		greetingService.retrieveFirstEaten(
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
					}
					public void onSuccess(final String results) {
						firstEaten.setText(results.equals("")?"Not Yet Eaten!!":results + " first eaten");
//						firstEaten.setStyleName("highlightAlertLabel");
						firstEaten.setHeight(height + "px");
					}
				});
	}

	private void populateDaysSinceBloated()	{
		greetingService.retrieveDaysSinceBloated(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
					}
					public void onSuccess(final Integer results) {
//						if (results!=null && results > 7)	{
							daysSinceBloated.setText(results!=null?results.toString() + " days since was bloated":"");
							daysSinceBloated.setHeight(height + "px");
//						}
					}
				});
	}

	private void populateDaysSinceBisacodyl()	{
		greetingService.retrieveDaysSinceBisacodyl(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
					}
					public void onSuccess(final Integer results) {
						daysSinceBisacodyl.setText(results!=null?results.toString() + " days since bisacodyl":"");
						daysSinceBisacodyl.setHeight(height + "px");
					}
				});
	}
//	private void passBackToParent()	{
//		if (alert1!=null && alert2!=null && alert3!=null && alert4!=null && alert5!=null && alert6!=null && alert7!=null && alert8!=null)	{
//			parentTab.addDigestionTab(alert1||alert2||alert3||alert4||alert5||alert6||alert7||alert8);
//		}
//	}
	@Override
	public String getTabTitle() {
		return tabTitle;
	}
	@Override
	public void setTabTitle(String tabTitle) {
		this.tabTitle = tabTitle;
	}
	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}
	@Override
	public void setTabIndex(Integer tabIndex) {
		this.tabIndex = tabIndex;
	}
	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}
}
